package com.lgx.mall.core.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.core.entity.Advertisement;
import com.lgx.mall.core.service.IAdvertisementService;
import com.lgx.right.entity.Organization;
import com.lgx.right.entity.User;
import com.lgx.right.service.IOrganizationService;


@Controller
@RequestMapping("/advertisement")
public class AdvertisementController {
	
	@Autowired
	private IAdvertisementService advertisementService;
	@Autowired
	private IOrganizationService organizationService;
	
	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public IAdvertisementService getAdvertisementService() {
		return advertisementService;
	}

	public void setAdvertisementService(IAdvertisementService advertisementService) {
		this.advertisementService = advertisementService;
	}
	
	@RequestMapping("/advertisementMgr.do")
	public String advertisementMgr(HttpServletRequest request,HttpServletResponse response)
	{
		return "mall/core/advertisement/advertisementMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		//查询当前商家
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		Pagination page = this.advertisementService.findAdvertisementPagination(new Object[]{user.getUserType(),user.getOrgId()}, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		
		Json2Utils.parseJSON(page, response);
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response)
	{
		String types = request.getParameter("types");
		String id = request.getParameter("id");
		if("edit".equals(types)){
			Advertisement advertisement = this.advertisementService.findAdvertisementById(Long.parseLong(id));
			request.setAttribute("advertisement", advertisement);
		}
		request.setAttribute("id", id);
		return "mall/core/advertisement/advertisement-init";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response)
	{
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(description))description = EscapeUtils.unescape(description);
		String imageUrl = request.getParameter("imageUrl");
		if(!StringUtils.isEmpty(imageUrl))imageUrl = EscapeUtils.unescape(imageUrl);
		String layerOrder = request.getParameter("layerOrder");
		if(!StringUtils.isEmpty(layerOrder))layerOrder = EscapeUtils.unescape(layerOrder);
		
		Advertisement advertisement = new Advertisement();
		
		advertisement.setDescription(description);
		advertisement.setImageUrl(imageUrl);
		advertisement.setLayerOrder(layerOrder);
		advertisement.setIsDelete("0");
		advertisement.setTimeLimit(0);
		
		String merchantId = request.getParameter("merchantId");
		if(!StringUtils.isEmpty(merchantId)){
			Organization org = this.organizationService.findOrganizationById(Long.parseLong(merchantId));
			advertisement.setMerchant(org);
		}
		
		try {
			this.advertisementService.createAdvertisement(advertisement);
			return "1";
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response)
	{	
		String id = request.getParameter("id");
		Advertisement advertisement = this.advertisementService.findAdvertisementById(Long.parseLong(id));
		
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(description))description = EscapeUtils.unescape(description);
		String imageUrl = request.getParameter("imageUrl");
		if(!StringUtils.isEmpty(imageUrl))imageUrl = EscapeUtils.unescape(imageUrl);
		String layerOrder = request.getParameter("layerOrder");
		if(!StringUtils.isEmpty(layerOrder))layerOrder = EscapeUtils.unescape(layerOrder);
		
		advertisement.setDescription(description);
		advertisement.setImageUrl(imageUrl);
		advertisement.setLayerOrder(layerOrder);
		advertisement.setIsDelete("0");
		advertisement.setTimeLimit(0);
		
		String merchantId = request.getParameter("merchantId");
		if(!StringUtils.isEmpty(merchantId)){
			Organization org = this.organizationService.findOrganizationById(Long.parseLong(merchantId));
			advertisement.setMerchant(org);
		}
		
		try {
			this.advertisementService.updateAdvertisement(advertisement);
			return "1";
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response)
	{
		String ids = request.getParameter("ids");
		try {
			this.advertisementService.deleteAdvertisements(ids.split(","));
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
}
